﻿using System;
using HalconDotNet;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using PylonLiveView;
using PylonC.NETSupportLibrary;

namespace PylonLiveView
{
    public partial class Form1 : Form
    {
        private ImageProvider m_imageProvider = new ImageProvider(); /* Create one image provider. */
        private Bitmap m_bitmap = null; /* The bitmap is used for displaying the image. */

        int flag = 0;
        int number = 0;

        HObject ho_Image = null, ho_Map = null;
        HTuple hv_WindowHandle = null;
        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
        URDateHandle URDateCollector = new URDateHandle();   //实例化数据类的对象
        URControlHandle URController = new URControlHandle();//实例化控制类的对象
        ModbusSocket URRegisterHandle = new ModbusSocket();  //实例化通信类的对象
        public static string theFirstCamIp = "";//
        public static string theSecondCamIp = "";
        public static uint theFirstCamIndex = 1;
        public static uint theSecondCamIndex = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stop();
            /* Close the image provider. */
            CloseTheImageProvider();

            try
            {
                /* Open the image provider using the index from the device data. */
                //m_imageProvider.Open(device.Index);
                m_imageProvider.Open(0);
                OneShot();
                flag = 6;
            }
            catch (Exception e1)
            {
                ShowException(e1, m_imageProvider.GetLastErrorMessage());
            }
        }

        /* Starts the grabbing of one image and handles exceptions. */
        private void OneShot()
        {
            try
            {
                m_imageProvider.OneShot(); /* Starts the grabbing of one image. */
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }

        /* Stops the image provider and handles exceptions. */
        private void Stop()
        {
            /* Stop the grabbing. */
            try
            {
                m_imageProvider.Stop();
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }

        /* Closes the image provider and handles exceptions. */
        private void CloseTheImageProvider()
        {
            /* Close the image provider. */
            try
            {
                m_imageProvider.Close();
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }

        /* Shows exceptions in a message box. */
        private void ShowException(Exception e, string additionalErrorMessage)
        {
            string more = "\n\nLast error message (may not belong to the exception):\n" + additionalErrorMessage;
            MessageBox.Show("Exception caught:\n" + e.Message + (additionalErrorMessage.Length > 0 ? more : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        HTuple hv_HomMat2D_1 = null, hv_Qx = null, hv_Qy = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    m_imageProvider.Open(0);
            //    // button1.Enabled = false;
            //    //ContinuousShot();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }


        /*
            函数：扫描相机
        */
        private void UpdateDeviceList()
        {
            try
            {
                /* Ask the device enumerator for a list of devices. */
                List<DeviceEnumerator.Device> list = DeviceEnumerator.EnumerateDevices();

                ListView.ListViewItemCollection items = deviceListView.Items;

                /* Add each new device to the list. */
                foreach (DeviceEnumerator.Device device in list)
                {
                    bool newitem = true;
                    /* For each enumerated device check whether it is in the list view. */
                    foreach (ListViewItem item in items)
                    {
                        /* Retrieve the device data from the list view item. */
                        DeviceEnumerator.Device tag = item.Tag as DeviceEnumerator.Device;

                        if (tag.FullName == device.FullName)
                        {
                            /* Update the device index. The index is used for opening the camera. It may change when enumerating devices. */
                            tag.Index = device.Index;
                            /* No new item needs to be added to the list view */
                            newitem = false;
                            break;
                        }
                    }

                    /* If the device is not in the list view yet the add it to the list view. */
                    if (newitem)
                    {
                        ListViewItem item = new ListViewItem(device.Name);
                        if (device.Tooltip.Length > 0)
                        {
                            item.ToolTipText = device.Tooltip;
                        }
                        item.Tag = device;

                        /* Attach the device data. */
                        deviceListView.Items.Add(item);
                    }
                    string[] ShowText = new string[3];
                    string strName = device.FullName;

                    string[] split1 = strName.Split('#');
                    string[] split2 = split1[2].Split(':');
                    ShowText[0] = device.Index.ToString();
                    ShowText[1] = split1[0];
                    ShowText[2] = split2[0];

                    if (device.Name.Substring(0, 1) == theFirstCamIp) theFirstCamIndex = device.Index;
                    if (device.Name.Substring(0, 1) == theSecondCamIp) theSecondCamIndex = device.Index;
                    ListViewItem li = new ListViewItem(ShowText);
                    deviceListView.Items.Add(li);
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }

        }

        private void button47_Click(object sender, EventArgs e)
        {
            UpdateDeviceList();
        }

        bool isSave = false;
        /* Handles the event related to an image having been taken and waiting for processing. */
        private void OnImageReadyEventCallback()
        {

            if (InvokeRequired)
            {
                /* If called from a different thread, we must use the Invoke method to marshal the call to the proper thread. */
                BeginInvoke(new ImageProvider.ImageReadyEventHandler(OnImageReadyEventCallback));
                return;
            }

            try
            {
                /* Acquire the image from the image provider. Only show the latest image. The camera may acquire images faster than images can be displayed*/
                ImageProvider.Image image = m_imageProvider.GetLatestImage();

                /* Check if the image has been removed in the meantime. */
                if (image != null)
                {
                    /* Check if the image is compatible with the currently used bitmap. */
                    if (BitmapFactory.IsCompatible(m_bitmap, image.Width, image.Height, image.Color))
                    {
                        /* Update the bitmap with the image data. */
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* To show the new image, request the display control to update itself. */
                        // pictureBox.Refresh();
                    }
                    else /* A new bitmap is required. */
                    {
                        BitmapFactory.CreateBitmap(out m_bitmap, image.Width, image.Height, image.Color);
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* We have to dispose the bitmap after assigning the new one to the display control. */
                        //Bitmap bitmap = pictureBox.Image as Bitmap;
                        ///* Provide the display control with the new bitmap. This action automatically updates the display. */
                        //pictureBox.Image = m_bitmap;
                        //if (bitmap != null)
                        //{
                        //    /* Dispose the bitmap. */
                        //    bitmap.Dispose();
                        //}
                    }

                    //HObject theImage = null;
                    HOperatorSet.GenEmptyObj(out ho_Image);
                    ho_Image.Dispose();
                    HTuple width, height;
                    BitmapToHObject(m_bitmap, out ho_Image);
                    //HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    HOperatorSet.GetImageSize(ho_Image, out width, out height);
                    HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, height, width);
                    HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);

                    /* The processing of the image is done. Release the image buffer. */
                    m_imageProvider.ReleaseImage();
                    /* The buffer can be used for the next image grabs. */

                    if (flag == 5)
                    {
                        flag = 0;
                        HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, height, width);
                        HOperatorSet.DispObj(ho_Image, hWindowControl2.HalconWindow);
                    }
                    if (flag == 6)
                    {
                        flag = 0;
                        HOperatorSet.SetPart(hWindowControl3.HalconWindow, 0, 0, height, width);
                        HOperatorSet.DispObj(ho_Image, hWindowControl3.HalconWindow);
                    }


                    if (isSave)
                    {
                        isSave = false;
                        ImageSave(ho_Image);
                    }
                    // URRegisterHandle.WriteMultipleRegister("0", 1, 128);
                    //底部图像检测
                    if (checkBox2.Checked && flag == 3)
                    {
                        flag = 0;
                        URRegisterHandle.WriteMultipleRegister("0", 1, 128);
                        HObject ho_Image1 = null, ho_GrayImage1 = null;
                        HObject ho_ROI1 = null, ho_ImageIC1 = null, ho_ImagePartIC1 = null;
                        HObject ho_ImageScaled = null, ho_Regions = null, ho_RegionDilation = null;
                        HObject ho_RegionOpening = null, ho_RegionFillUp = null, ho_RegionDifference = null;
                        HObject ho_ConnectedRegions = null, ho_SelectedRegions = null;

                        // Local control variables 


                        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
                        HTuple hv_WindowHandle = new HTuple(), hv_Area1 = new HTuple();
                        HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
                        HTuple hv_Area2 = new HTuple(), hv_Row2 = new HTuple();
                        HTuple hv_Column2 = new HTuple(), hv_Area = new HTuple();
                        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
                        // Initialize local and output iconic variables 
                        HOperatorSet.GenEmptyObj(out ho_Image1);
                        HOperatorSet.GenEmptyObj(out ho_GrayImage1);
                        HOperatorSet.GenEmptyObj(out ho_ROI1);
                        HOperatorSet.GenEmptyObj(out ho_ImageIC1);
                        HOperatorSet.GenEmptyObj(out ho_ImagePartIC1);
                        HOperatorSet.GenEmptyObj(out ho_ImageScaled);
                        HOperatorSet.GenEmptyObj(out ho_Regions);
                        HOperatorSet.GenEmptyObj(out ho_RegionDilation);
                        HOperatorSet.GenEmptyObj(out ho_RegionOpening);
                        HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
                        HOperatorSet.GenEmptyObj(out ho_RegionDifference);
                        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
                        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
                        try
                        {
                            //图片路径自己加

                            // dev_update_window(...); only in hdevelop
                            //if (HDevWindowStack.IsOpen())
                            //{
                            //    HOperatorSet.CloseWindow(HDevWindowStack.Pop());
                            //}
                            //ho_Image.Dispose();
                            //HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_Index));
                            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                            HOperatorSet.SetWindowAttr("background_color", "black");
                            //HOperatorSet.OpenWindow(0, 0, hv_Width / 4, hv_Height / 4, 0, "visible", "", out hv_WindowHandle);
                            //HDevWindowStack.Push(hv_WindowHandle);
                            //HDevWindowStack.SetActive(hv_WindowHandle);
                            if (HDevWindowStack.IsOpen())
                            {
                                //HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                            }
                            ho_GrayImage1.Dispose();
                            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage1);
                            ho_ROI1.Dispose();
                            HOperatorSet.GenRectangle1(out ho_ROI1, 715.5, 641.5, 1523.5, 1549.5);

                            ho_ImageIC1.Dispose();
                            HOperatorSet.ReduceDomain(ho_GrayImage1, ho_ROI1, out ho_ImageIC1);
                            ho_ImagePartIC1.Dispose();
                            HOperatorSet.CropDomain(ho_ImageIC1, out ho_ImagePartIC1);

                            ho_ImageScaled.Dispose();
                            HOperatorSet.ScaleImage(ho_ImagePartIC1, out ho_ImageScaled, 3.49315, -636);
                            ho_Regions.Dispose();
                            HOperatorSet.Threshold(ho_ImageScaled, out ho_Regions, 0, 221);
                            HOperatorSet.AreaCenter(ho_Regions, out hv_Area1, out hv_Row1, out hv_Column1);

                            ho_RegionDilation.Dispose();
                            HOperatorSet.DilationRectangle1(ho_Regions, out ho_RegionDilation, 3, 3);
                            ho_RegionOpening.Dispose();
                            HOperatorSet.OpeningRectangle1(ho_RegionDilation, out ho_RegionOpening, 2,
                                2);
                            HOperatorSet.AreaCenter(ho_RegionOpening, out hv_Area2, out hv_Row2, out hv_Column2);

                            ho_RegionFillUp.Dispose();
                            HOperatorSet.FillUp(ho_RegionOpening, out ho_RegionFillUp);

                            ho_RegionDifference.Dispose();
                            HOperatorSet.Difference(ho_RegionFillUp, ho_RegionOpening, out ho_RegionDifference
                                );
                            ho_ConnectedRegions.Dispose();
                            HOperatorSet.Connection(ho_RegionDifference, out ho_ConnectedRegions);
                            ho_SelectedRegions.Dispose();
                            HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions,
                                "max_area", 70);
                            HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column);
                            if ((int)(new HTuple(hv_Area.TupleGreater(10))) != 0)
                            {
                                if (HDevWindowStack.IsOpen())
                                {
                                    HOperatorSet.DispObj(ho_ImagePartIC1, HDevWindowStack.GetActive());
                                }
                                label12.ForeColor = Color.Red;
                                //label12.BackColor = Color.Red;
                                label12.Text = "不合格";
                                //set_display_font(hv_WindowHandle, 30, "mono", "true", "false");
                                //disp_message(hv_WindowHandle, "不合格", "window", 0, 0, "red", "true");
                            }
                            else
                            {
                                if (HDevWindowStack.IsOpen())
                                {
                                    HOperatorSet.DispObj(ho_ImagePartIC1, HDevWindowStack.GetActive());
                                }
                                //set_display_font(hv_WindowHandle, 30, "mono", "true", "false");
                                //disp_message(hv_WindowHandle, "合格", "window", 0, 0, "green", "true");
                                label12.ForeColor = Color.Lime;
                                //label12.BackColor = Color.Red;
                                label12.Text = "合格";
                            }
                            //set_display_font(hv_WindowHandle, 18, "mono", "true", "false");
                            //disp_continue_message(hv_WindowHandle, "black", "true");
                            // stop(...); only in hdevelop
                        }
                        catch (Exception e)
                        {
                            ShowException(e, m_imageProvider.GetLastErrorMessage());
                        }
                        ho_Image.Dispose();
                        ho_GrayImage1.Dispose();
                        ho_ROI1.Dispose();
                        ho_ImageIC1.Dispose();
                        ho_ImagePartIC1.Dispose();
                        ho_ImageScaled.Dispose();
                        ho_Regions.Dispose();
                        ho_RegionDilation.Dispose();
                        ho_RegionOpening.Dispose();
                        ho_RegionFillUp.Dispose();
                        ho_RegionDifference.Dispose();
                        ho_ConnectedRegions.Dispose();
                        ho_SelectedRegions.Dispose();
                    }
                    //底部图像测量
                    if (checkBox2.Checked && flag == 4)
                    {
                        flag = 0;
                        URRegisterHandle.WriteMultipleRegister("0", 1, 128);
                        HObject ho_Image1 = null, ho_GrayImage1 = null;
                        HObject ho_ROI1 = null, ho_ImageIC1 = null, ho_ImagePartIC1 = null;
                        HObject ho_ImageScaled1 = null, ho_Region1 = null, ho_RegionOpening1 = null;
                        HObject ho_RegionUnion = null, ho_RegionFillUp = null, ho_RegionDifference = null;
                        HObject ho_ConnectedRegions = null, ho_SelectedRegions = null;

                        // Local control variables 


                        HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
                        HTuple hv_WindowHandle = new HTuple(), hv_Area = new HTuple();
                        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
                        // Initialize local and output iconic variables 
                        HOperatorSet.GenEmptyObj(out ho_Image1);
                        HOperatorSet.GenEmptyObj(out ho_GrayImage1);
                        HOperatorSet.GenEmptyObj(out ho_ROI1);
                        HOperatorSet.GenEmptyObj(out ho_ImageIC1);
                        HOperatorSet.GenEmptyObj(out ho_ImagePartIC1);
                        HOperatorSet.GenEmptyObj(out ho_ImageScaled1);
                        HOperatorSet.GenEmptyObj(out ho_Region1);
                        HOperatorSet.GenEmptyObj(out ho_RegionOpening1);
                        HOperatorSet.GenEmptyObj(out ho_RegionUnion);
                        HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
                        HOperatorSet.GenEmptyObj(out ho_RegionDifference);
                        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
                        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
                        try
                        {
                            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                            HOperatorSet.SetWindowAttr("background_color", "black");
                            // HOperatorSet.OpenWindow(0, 0, hv_Width / 4, hv_Height / 4, 0, "visible", "", out hv_WindowHandle);
                            // HDevWindowStack.Push(hv_WindowHandle);
                            //dev_set_window (WindowHandle)
                            if (HDevWindowStack.IsOpen())
                            {
                                //dev_display (Image)
                            }
                            ho_GrayImage1.Dispose();
                            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage1);
                            //gen_rectangle1 (ROI1, 530, 880, 1400, 1700)
                            ho_ROI1.Dispose();
                            HOperatorSet.GenRectangle1(out ho_ROI1, 569.5, 617.5, 1457.5, 1617.5);
                            ho_ImageIC1.Dispose();
                            HOperatorSet.ReduceDomain(ho_GrayImage1, ho_ROI1, out ho_ImageIC1);
                            ho_ImagePartIC1.Dispose();
                            HOperatorSet.CropDomain(ho_ImageIC1, out ho_ImagePartIC1);

                            ho_ImageScaled1.Dispose();
                            HOperatorSet.ScaleImage(ho_ImagePartIC1, out ho_ImageScaled1, 3.69565, -687);
                            ho_Region1.Dispose();
                            HOperatorSet.BinThreshold(ho_ImageScaled1, out ho_Region1);


                            //closing_rectangle1 (Region1, RegionClosing1, 2, 2)
                            //opening_rectangle1 (RegionClosing1, RegionOpening1, 1, 1)
                            ho_RegionOpening1.Dispose();
                            HOperatorSet.OpeningRectangle1(ho_Region1, out ho_RegionOpening1, 1, 1);
                            ho_RegionUnion.Dispose();
                            HOperatorSet.Union1(ho_RegionOpening1, out ho_RegionUnion);
                            ho_RegionFillUp.Dispose();
                            HOperatorSet.FillUp(ho_RegionUnion, out ho_RegionFillUp);

                            ho_RegionDifference.Dispose();
                            HOperatorSet.Difference(ho_RegionFillUp, ho_RegionOpening1, out ho_RegionDifference
                                );
                            ho_ConnectedRegions.Dispose();
                            HOperatorSet.Connection(ho_RegionDifference, out ho_ConnectedRegions);
                            ho_SelectedRegions.Dispose();
                            HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions,
                                "max_area", 70);
                            HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column);
                            if ((int)(new HTuple(hv_Area.TupleGreater(10))) != 0)
                            {
                                if (HDevWindowStack.IsOpen())
                                {
                                    HOperatorSet.DispObj(ho_ImagePartIC1, HDevWindowStack.GetActive());
                                }
                                label12.ForeColor = Color.Red;
                                //label12.BackColor = Color.Red;
                                label12.Text = "不合格";
                                //set_display_font(hv_WindowHandle, 30, "mono", "true", "false");
                                // disp_message(hv_WindowHandle, "不合格", "window", 0, 0, "red", "true");
                            }
                            else
                            {
                                if (HDevWindowStack.IsOpen())
                                {
                                    HOperatorSet.DispObj(ho_ImagePartIC1, HDevWindowStack.GetActive());
                                }
                                label12.ForeColor = Color.Lime;
                                label12.Text = "合格";
                                // set_display_font(hv_WindowHandle, 30, "mono", "true", "false");
                                // disp_message(hv_WindowHandle, "合格", "window", 0, 0, "green", "true");

                            }

                        }
                        catch (Exception e)
                        {
                            ShowException(e, m_imageProvider.GetLastErrorMessage());
                        }

                        ho_Image.Dispose();
                        ho_GrayImage1.Dispose();
                        ho_ROI1.Dispose();
                        ho_ImageIC1.Dispose();
                        ho_ImagePartIC1.Dispose();
                        ho_ImageScaled1.Dispose();
                        ho_Region1.Dispose();
                        ho_RegionOpening1.Dispose();
                        ho_RegionUnion.Dispose();
                        ho_RegionFillUp.Dispose();
                        ho_RegionDifference.Dispose();
                        ho_ConnectedRegions.Dispose();
                        ho_SelectedRegions.Dispose();
                    }
                    //识别测量算法
                    if (checkBox1.Checked && flag == 1)
                    {
                        flag = 0;
                        URRegisterHandle.WriteMultipleRegister("0", 1, 128);
                        number++;
                        label13.Text = "正在加工第" + number + "个芯片";
                        HObject ho_Regions = null, ho_ConnectedRegions = null;
                        HObject ho_SelectedRegions = null, ho_ImageReduced1 = null, ho_ImageReduced2 = null, ho_ROI_0 = null, ho_Rectangle = null, ho_RegionDilation = null;
                        HObject ho_ImageAffineTrans = null, ho_RegionAffineTrans = null;
                        HObject ho_ImageReduced = null, ho_ImageMapped = null;
                        HTuple hv_Row = new HTuple();
                        HTuple hv_Column = new HTuple(), hv_Phi = new HTuple();
                        HTuple hv_Length1 = new HTuple(), hv_Length2 = new HTuple();
                        HTuple hv_Area = new HTuple(), hv_Row1 = new HTuple();
                        HTuple hv_Column1 = new HTuple(), hv_Phi1 = new HTuple();
                        HTuple hv_HomMat2D = new HTuple(), hv_CameraParameters = new HTuple();
                        HTuple hv_CameraPose = new HTuple(), hv_AmplitudeThreshold = new HTuple();
                        HTuple hv_RoiWidthLen2 = new HTuple(), hv_LineRowStart_Measure_01_0 = new HTuple();
                        HTuple hv_LineColumnStart_Measure_01_0 = new HTuple();
                        HTuple hv_LineRowEnd_Measure_01_0 = new HTuple(), hv_LineColumnEnd_Measure_01_0 = new HTuple();
                        HTuple hv_TmpCtrl_Row = new HTuple(), hv_TmpCtrl_Column = new HTuple();
                        HTuple hv_TmpCtrl_Dr = new HTuple(), hv_TmpCtrl_Dc = new HTuple();
                        HTuple hv_TmpCtrl_Phi = new HTuple(), hv_TmpCtrl_Len1 = new HTuple();
                        HTuple hv_TmpCtrl_Len2 = new HTuple(), hv_MsrHandle_Measure_01_0 = new HTuple();
                        HTuple hv_Row_Measure_01_0 = new HTuple(), hv_Column_Measure_01_0 = new HTuple();
                        HTuple hv_Amplitude_Measure_01_0 = new HTuple(), hv_Distance_Measure_01_0 = new HTuple();
                        HTuple hv_Column_World_Measure_01_0 = new HTuple(), hv_Row_World_Measure_01_0 = new HTuple();
                        HTuple hv_TmpCtrl_Length = new HTuple(), hv_TmpCtrl_RowFrom = new HTuple();
                        HTuple hv_TmpCtrl_ColumnFrom = new HTuple(), hv_TmpCtrl_RowTo = new HTuple();
                        HTuple hv_TmpCtrl_ColumnTo = new HTuple(), hv_Distance_World_Measure_01_0 = new HTuple();
                        HTuple hv_distance_changdu = new HTuple(), hv_distance_banchang = new HTuple(), hv_distance_guanjiao = new HTuple();
                        HTuple hv_distance_tangxi = new HTuple();

                        HTuple hv_Column_World_Measure_01_1 = null, hv_Row_World_Measure_01_1 = null;
                        HTuple hv_Distance_World_Measure_01_1 = new HTuple();
                        HTuple hv_Distance_World_Measure_01_2 = new HTuple();
                        HTuple hv_Distance_World_Measure_01_3 = new HTuple();
                        HTuple hv_Distance_World_Measure_01_4 = new HTuple();

                        HTuple hv_LineRowStart_Measure_01_1 = null;
                        HTuple hv_LineColumnStart_Measure_01_1 = null, hv_LineRowEnd_Measure_01_1 = null;
                        HTuple hv_LineColumnEnd_Measure_01_1 = null, hv_MsrHandle_Measure_01_1 = null;

                        HTuple hv_CamParamOut = null, deg = new HTuple();
                        HTuple hv_distance0 = new HTuple();
                        HTuple hv_distance1 = new HTuple(), hv_distance2 = new HTuple();
                        HTuple hv_distance3 = new HTuple(), hv_distance4 = new HTuple();
                        HTuple hv_distance5 = new HTuple(), hv_distance6 = new HTuple();

                        //  HTuple hv_Row_Measure_01_0 = null, hv_Column_Measure_01_0 = null;
                        //  HTuple hv_Amplitude_Measure_01_0 = null, hv_Distance_Measure_01_0 = null;
                        HTuple hv_Row_Measure_01_1 = null, hv_Column_Measure_01_1 = null;
                        HTuple hv_Amplitude_Measure_01_1 = null, hv_Distance_Measure_01_1 = null;

                        HTuple hv_Distance_World_Measure_01_5 = new HTuple();
                        HOperatorSet.GenEmptyObj(out ho_Map);
                        HOperatorSet.GenEmptyObj(out ho_ImageMapped);

                        hv_CameraParameters = new HTuple();
                        hv_CameraParameters[0] = "area_scan_division";
                        hv_CameraParameters[1] = 0.0218374;
                        hv_CameraParameters[2] = -635.191;
                        hv_CameraParameters[3] = 3.44956e-006;
                        hv_CameraParameters[4] = 3.45e-006;
                        hv_CameraParameters[5] = 1253.91;
                        hv_CameraParameters[6] = 965.231;
                        hv_CameraParameters[7] = 2448;
                        hv_CameraParameters[8] = 2048;
                        hv_CameraPose = new HTuple();
                        hv_CameraPose[0] = 0.00227568;
                        hv_CameraPose[1] = -0.00148985;
                        hv_CameraPose[2] = 0.179617;
                        hv_CameraPose[3] = 0.25094;
                        hv_CameraPose[4] = 0.545408;
                        hv_CameraPose[5] = 270.642;
                        hv_CameraPose[6] = 0;

                        // Initialize local and output iconic variables 
                        //HOperatorSet.GenEmptyObj(out ho_Image);
                        HOperatorSet.GenEmptyObj(out ho_ROI_0);
                        HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
                        HOperatorSet.GenEmptyObj(out ho_Regions);
                        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
                        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
                        HOperatorSet.GenEmptyObj(out ho_Rectangle);
                        HOperatorSet.GenEmptyObj(out ho_RegionDilation);
                        HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans);
                        HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans);
                        HOperatorSet.GenEmptyObj(out ho_ImageReduced);

                        HOperatorSet.ChangeRadialDistortionCamPar("adaptive", hv_CameraParameters, 0, out hv_CamParamOut);
                        ho_Map.Dispose();
                        HOperatorSet.GenRadialDistortionMap(out ho_Map, hv_CameraParameters, hv_CamParamOut, "bilinear");
                        ho_ImageMapped.Dispose();
                        HOperatorSet.MapImage(ho_Image, ho_Map, out ho_ImageMapped);

                        ho_ROI_0.Dispose();
                        //HOperatorSet.GenCircle(out ho_ROI_0, 952.5, 1302.5, 986.7);
                        HOperatorSet.GenCircle(out ho_ROI_0, 957.5, 1277.5, 1039.92);
                        ho_ImageReduced1.Dispose();
                        ho_Regions.Dispose();

                        HOperatorSet.ReduceDomain(ho_ImageMapped, ho_ROI_0, out ho_ImageReduced);
                        //HOperatorSet.Threshold(ho_ImageReduced1, out ho_Regions, 0, 205);
                        HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 6, 196);
                        //HOperatorSet.Threshold(ho_ImageReduced1, out ho_Regions, 0, 205);

                        HOperatorSet.SmallestRectangle2(ho_Regions, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2);
                        ho_Rectangle.Dispose();
                        HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2);
                        HOperatorSet.AreaCenter(ho_Rectangle, out hv_Area, out hv_Row1, out hv_Column1);
                        HOperatorSet.OrientationRegion(ho_Rectangle, out hv_Phi1);


                        if (hv_Phi > 0)
                        {
                            textBox17.Text = Math.Round((double)hv_Phi.TupleDeg(), 2).ToString();
                            textBox18.Text = Math.Round((double)hv_Phi, 2).ToString();
                            deg = hv_Phi.TupleDeg();
                            double deg1 = Math.Round((double)hv_Phi, 2);
                            URRegisterHandle.WriteMultipleRegister((deg1 * 100).ToString(), 1, 132);
                        }
                        else
                        {
                            textBox17.Text = Math.Round((double)hv_Phi.TupleDeg(), 2).ToString();
                            textBox18.Text = Math.Round((double)hv_Phi, 2).ToString();
                            deg = hv_Phi.TupleDeg();
                            double deg1 = Math.Round((double)hv_Phi, 2);
                            URRegisterHandle.WriteMultipleRegister((deg1 * (-100) + 1000).ToString(), 1, 132);
                        }

                        HOperatorSet.VectorAngleToRigid(hv_Row1, hv_Column1, hv_Phi, hv_Row1, hv_Column1, (new HTuple(0)).TupleRad(), out hv_HomMat2D);
                        ho_ImageAffineTrans.Dispose();
                        HOperatorSet.AffineTransImage(ho_ImageReduced, out ho_ImageAffineTrans, hv_HomMat2D, "constant", "false");
                        ho_RegionAffineTrans.Dispose();
                        HOperatorSet.AffineTransRegion(ho_Rectangle, out ho_RegionAffineTrans, hv_HomMat2D, "nearest_neighbor");
                        ho_RegionDilation.Dispose();
                        HOperatorSet.DilationCircle(ho_RegionAffineTrans, out ho_RegionDilation, 5);
                        ho_ImageReduced1.Dispose();
                        HOperatorSet.ReduceDomain(ho_ImageAffineTrans, ho_RegionDilation, out ho_ImageReduced1);

                        //HOperatorSet.AreaCenter(ho_RegionDilation, out hv_Area, out hv_Row1, out hv_Column1);
                        //显示中心坐标
                        textBox12.Text = Math.Round((double)hv_Row1, 2).ToString();
                        textBox13.Text = Math.Round((double)hv_Column1, 2).ToString();

                        //hv_HomMat2D_1 = new HTuple();
                        //hv_HomMat2D_1[0] = -9.60583e-005;
                        //hv_HomMat2D_1[1] = -0.0307922;
                        //hv_HomMat2D_1[2] = -232.873;
                        //hv_HomMat2D_1[3] = 0.0308407;
                        //hv_HomMat2D_1[4] = -0.000174769;
                        //hv_HomMat2D_1[5] = 328.651;

                        hv_HomMat2D_1 = new HTuple();
                        hv_HomMat2D_1[0] = -0.0281043;
                        hv_HomMat2D_1[1] = -6.99041e-005;
                        hv_HomMat2D_1[2] = -234.018;
                        hv_HomMat2D_1[3] = -0.000148101;
                        hv_HomMat2D_1[4] = 0.0281748;
                        hv_HomMat2D_1[5] = 334.451;

                        //double distance_banjing0 = Math.Round((double)hv_distance_banchang, 2);
                        HOperatorSet.AffineTransPoint2d(hv_HomMat2D_1, float.Parse(textBox13.Text), float.Parse(textBox12.Text), out hv_Qx, out hv_Qy);
                        //HOperatorSet.AffineTransPoint2d(hv_HomMat2D_1, 1327.96, 1093.51, out hv_Qx, out hv_Qy);
                        textBox17.Text = Math.Round((double)hv_Qx, 4).ToString();
                        textBox18.Text = Math.Round((double)hv_Qy, 4).ToString();
                        double x = Math.Round((double)hv_Qx, 2);
                        double y = Math.Round((double)(hv_Qy), 2);
                        URRegisterHandle.WriteMultipleRegister((x * (-100)).ToString(), 1, 130);
                        URRegisterHandle.WriteMultipleRegister((y * 100).ToString(), 1, 131);


                        //Measure 01: Code generated by Measure 01
                        //Measure 01: Initialize calibration
                        hv_CameraParameters = new HTuple();
                        hv_CameraParameters[0] = "area_scan_division";
                        hv_CameraParameters[1] = 0.0168634;
                        hv_CameraParameters[2] = -702.175;
                        hv_CameraParameters[3] = 3.45005e-006;
                        hv_CameraParameters[4] = 3.45e-006;
                        hv_CameraParameters[5] = 1246.34;
                        hv_CameraParameters[6] = 946.581;
                        hv_CameraParameters[7] = 2448;
                        hv_CameraParameters[8] = 2048;
                        hv_CameraPose = new HTuple();
                        hv_CameraPose[0] = -0.00900705;
                        hv_CameraPose[1] = -0.00658053;
                        hv_CameraPose[2] = 0.136042;
                        hv_CameraPose[3] = 359.118;
                        hv_CameraPose[4] = 1.51868;
                        hv_CameraPose[5] = 91.2157;
                        hv_CameraPose[6] = 0;
                        //Measure 01: Prepare measurement
                        hv_AmplitudeThreshold = 39;
                        hv_RoiWidthLen2 = 48.5;
                        HOperatorSet.SetSystem("int_zooming", "true");
                        //Measure 01: Coordinates for line Measure 01 [0]
                        hv_LineRowStart_Measure_01_0 = 939.91;
                        hv_LineColumnStart_Measure_01_0 = 479.581;
                        hv_LineRowEnd_Measure_01_0 = 938.496;
                        hv_LineColumnEnd_Measure_01_0 = 2014.01;
                        //Measure 01: Convert coordinates to rectangle2 type
                        hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_0 + hv_LineRowEnd_Measure_01_0);
                        hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_0 + hv_LineColumnEnd_Measure_01_0);
                        hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_0 - hv_LineRowEnd_Measure_01_0;
                        hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_0 - hv_LineColumnStart_Measure_01_0;
                        hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
                        hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                            );
                        hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
                        //Measure 01: Create measure for line Measure 01 [0]
                        //Measure 01: Attention: This assumes all images have the same size!
                        HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                            hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_0);
                        //Measure 01: Coordinates for line Measure 01 [1]
                        hv_LineRowStart_Measure_01_1 = 163.5;
                        hv_LineColumnStart_Measure_01_1 = 1251.05;
                        hv_LineRowEnd_Measure_01_1 = 1765.5;
                        hv_LineColumnEnd_Measure_01_1 = 1245.05;
                        //Measure 01: Convert coordinates to rectangle2 type
                        hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_1 + hv_LineRowEnd_Measure_01_1);
                        hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_1 + hv_LineColumnEnd_Measure_01_1);
                        hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_1 - hv_LineRowEnd_Measure_01_1;
                        hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_1 - hv_LineColumnStart_Measure_01_1;
                        hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
                        hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                            );
                        hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
                        //Measure 01: Create measure for line Measure 01 [1]
                        //Measure 01: Attention: This assumes all images have the same size!
                        HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                            hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_1);
                        //Measure 01: ***************************************************************
                        //Measure 01: * The code which follows is to be executed once / measurement *
                        //Measure 01: ***************************************************************
                        //Measure 01: The image is assumed to be made available in the
                        //Measure 01: variable last displayed in the graphics window
                        ho_Image.Dispose();
                        HOperatorSet.CopyObj(ho_ImageReduced1, out ho_Image, 1, 1);
                        //Measure 01: Execute measurements
                        HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_0, 5.3, hv_AmplitudeThreshold,
                            "all", "all", out hv_Row_Measure_01_0, out hv_Column_Measure_01_0, out hv_Amplitude_Measure_01_0,
                            out hv_Distance_Measure_01_0);
                        HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_1, 5.3, hv_AmplitudeThreshold,
                            "all", "all", out hv_Row_Measure_01_1, out hv_Column_Measure_01_1, out hv_Amplitude_Measure_01_1,
                            out hv_Distance_Measure_01_1);
                        //Measure 01: Transform to world coordinates
                        //Measure 01: Calibrate positions for Measure 01 [0]
                        HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_0,
                            hv_Column_Measure_01_0, 0.001, out hv_Column_World_Measure_01_0, out hv_Row_World_Measure_01_0);
                        //Measure 01: Calibrate distances
                        hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_0.TupleLength());
                        if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
                        {
                            HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_0, 0, hv_TmpCtrl_Length - 2,
                                out hv_TmpCtrl_RowFrom);
                            HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_0, 0, hv_TmpCtrl_Length - 2,
                                out hv_TmpCtrl_ColumnFrom);
                            HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_0, 1, hv_TmpCtrl_Length - 1,
                                out hv_TmpCtrl_RowTo);
                            HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_0, 1, hv_TmpCtrl_Length - 1,
                                out hv_TmpCtrl_ColumnTo);
                            HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                                hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_0);
                        }
                        //Measure 01: Calibrate positions for Measure 01 [1]
                        HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_1,
                            hv_Column_Measure_01_1, 0.001, out hv_Column_World_Measure_01_1, out hv_Row_World_Measure_01_1);
                        //Measure 01: Calibrate distances
                        hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_1.TupleLength());
                        if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
                        {
                            HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_1, 0, hv_TmpCtrl_Length - 2,
                                out hv_TmpCtrl_RowFrom);
                            HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_1, 0, hv_TmpCtrl_Length - 2,
                                out hv_TmpCtrl_ColumnFrom);
                            HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_1, 1, hv_TmpCtrl_Length - 1,
                                out hv_TmpCtrl_RowTo);
                            HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_1, 1, hv_TmpCtrl_Length - 1,
                                out hv_TmpCtrl_ColumnTo);
                            HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                                hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_1);
                        }
                        //Measure 01: Do something with the results
                        //Measure 01: Clear measure when done
                        HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_0);
                        HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_1);


                        hv_distance_changdu = hv_Distance_World_Measure_01_1.TupleSum();
                        hv_distance_banchang = (hv_Distance_World_Measure_01_0.TupleSum()) / 2;
                        hv_distance_guanjiao = ((((hv_Distance_World_Measure_01_0.TupleSelect(0)) + (hv_Distance_World_Measure_01_0.TupleSelect(
                            2)))).TupleSum()) / 2;


                        //hv_distance_changdu = hv_distance0;
                        // hv_distance_banchang = (hv_distance0.TupleSum()) / 2;
                        double distance_banjing0 = Math.Round((double)hv_distance_banchang, 2);
                        double distance0 = Math.Round((double)hv_distance_changdu, 2);
                        double distance_banjing2 = Math.Round((double)hv_distance_banchang, 3);
                        double distance2 = Math.Round((double)hv_distance_changdu, 3);
                        this.label8.Text = distance2.ToString() + "mm";
                        this.label9.Text = distance_banjing2.ToString() + "mm";
                        URRegisterHandle.WriteMultipleRegister((distance0 * 100).ToString(), 1, 129);

                        m_imageProvider.ReleaseImage();
                        /* The buffer can be used for the next image grabs. */
                    }
                    //识别吸头中心点坐标
                    if (checkBox4.Checked)
                    {
                        HObject ho_ImageReduced = null, ho_SelectedRegions = null, ho_ImageMapped = null, ho_ROI_0 = null, ho_ImageReduced1 = null, ho_Regions = null, ho_ConnectedRegions = null;
                        HTuple hv_CamParamOut = null;
                        HTuple hv_HomMat2D = new HTuple(), hv_CameraParameters = new HTuple();
                        HTuple hv_CameraPose = new HTuple(), hv_AmplitudeThreshold = new HTuple();

                        HTuple hv_AcqHandle = null, hv_Area = new HTuple();
                        HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
                        HTuple hv_HomMat2D_1 = new HTuple(), hv_Qx = new HTuple();
                        HTuple hv_Qy = new HTuple();


                        HOperatorSet.GenEmptyObj(out ho_Map);
                        HOperatorSet.GenEmptyObj(out ho_ImageMapped);
                        HOperatorSet.GenEmptyObj(out ho_Regions);
                        HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
                        HOperatorSet.GenEmptyObj(out ho_SelectedRegions);


                        hv_CameraParameters = new HTuple();
                        hv_CameraParameters[0] = "area_scan_division";
                        hv_CameraParameters[1] = 0.0168634;
                        hv_CameraParameters[2] = -702.175;
                        hv_CameraParameters[3] = 3.45005e-006;
                        hv_CameraParameters[4] = 3.45e-006;
                        hv_CameraParameters[5] = 1246.34;
                        hv_CameraParameters[6] = 946.581;
                        hv_CameraParameters[7] = 2448;
                        hv_CameraParameters[8] = 2048;
                        hv_CameraPose = new HTuple();
                        hv_CameraPose[0] = -0.00900705;
                        hv_CameraPose[1] = -0.00658053;
                        hv_CameraPose[2] = 0.136042;
                        hv_CameraPose[3] = 359.118;
                        hv_CameraPose[4] = 1.51868;
                        hv_CameraPose[5] = 91.2157;
                        hv_CameraPose[6] = 0;

                        HOperatorSet.ChangeRadialDistortionCamPar("adaptive", hv_CameraParameters, 0, out hv_CamParamOut);
                        ho_Map.Dispose();
                        HOperatorSet.GenRadialDistortionMap(out ho_Map, hv_CameraParameters, hv_CamParamOut, "bilinear");
                        ho_ImageMapped.Dispose();
                        HOperatorSet.MapImage(ho_Image, ho_Map, out ho_ImageMapped);



                        //识别吸头中心点
                        ho_Regions.Dispose();
                        HOperatorSet.Threshold(ho_ImageMapped, out ho_Regions, 0, 42);
                        ho_ConnectedRegions.Dispose();
                        HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);

                        ho_SelectedRegions.Dispose();
                        //HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("area")).TupleConcat(
                        //    "roundness"), "and", (new HTuple(63848.9)).TupleConcat(0.6853), (new HTuple(162770)).TupleConcat(
                        //    1));
                        HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("area")).TupleConcat(
    "roundness"), "and", (new HTuple(37811.4)).TupleConcat(0.70018), (new HTuple(68060.5)).TupleConcat(
    1));
                        HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column);


                        hv_HomMat2D_1 = new HTuple();
                        hv_HomMat2D_1[0] = -0.0281043;
                        hv_HomMat2D_1[1] = -6.99041e-005;
                        hv_HomMat2D_1[2] = -234.018;
                        hv_HomMat2D_1[3] = -0.000148101;
                        hv_HomMat2D_1[4] = 0.0281748;
                        hv_HomMat2D_1[5] = 334.451;

                        //HomMat2D_1 := [0.0441368, 0.00123403, 343.785, -0.00114995, 0.0438927, -497.576]
                        HOperatorSet.AffineTransPoint2d(hv_HomMat2D_1, hv_Row, hv_Column, out hv_Qx, out hv_Qy);

                        textBox10.Text = Math.Round((double)hv_Qx, 4).ToString();
                        textBox11.Text = Math.Round((double)hv_Qy, 4).ToString();
                    }

                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }
        /// 将bitmap转化为Hobject
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="image"></param>
        public void BitmapToHObject(Bitmap bmp, out HObject image)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData srcBmData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                System.IntPtr srcPtr = srcBmData.Scan0;
                HOperatorSet.GenImageInterleaved(out image, srcPtr, "bgrx", bmp.Width, bmp.Height, -1, "byte", 0, 0, 0, 0, -1, 0);
                bmp.UnlockBits(srcBmData);
            }
            catch (Exception ex)
            {
                image = null;
                MessageBox.Show(ex.Message);
            }
        }

        /*
           函数：储存图片
         */
        public void ImageSave(HObject ho_Image)
        {
            string fileName = DateTime.Now.ToString().Replace(':', ' ').Trim();
            fileName = fileName.Replace('/', ' ');
            if (ho_Image == null) return;
            //HOperatorSet.WriteImage(ho_Image, "bmp", 0, "D:/testpic/" + fileName + ".bmp");
            HOperatorSet.WriteImage(ho_Image, "bmp", 0, "D:/testpic1/22.bmp");
        }
    }
}
